package com.jfinal.j2cache;

import com.jfinal.j2cache.util.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CacheArgsMapping {

    private static final Logger logger = LoggerFactory.getLogger(CacheArgsMapping.class);

    private static Map<String, Map<String, Integer>> metadataCache = new ConcurrentHashMap<String, Map<String, Integer>>();

    public static Integer getPosOfArgs(Class<?> clazz, Method method, String argsName) {
        String className = clazz.getName();
        int $pos = 0;
        if (($pos = className.indexOf("$")) != -1) {
            className = className.substring(0, $pos);
        }

        String key = className + "#" + method.getName();

        logger.debug("class#method: {}", key);

        Map<String, Integer> map = metadataCache.get(key);
        if (null != map) {
            return map.get(argsName);
        }

        map = ClassUtils.getMethodParamNames(method);
        metadataCache.put(key, map);
        return map.get(argsName);
    }
}
